package com.imooc.security.core.validate.code;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.Map;

/**
 * @author sshhu
 */
public abstract class AbstractValidateCodeProesccor<C extends ValidateCode> implements ValidateCodeProcessor{
    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    @Autowired
    private Map<String,ValidateCodeGenerator> validateCodeGenerator;

    @Override
    public void create(ServletWebRequest request,String type) {
        String simpleName = ValidateCodeGenerator.class.getSimpleName();
        String name = type+simpleName;
        ValidateCodeGenerator validateCodeGenerator = this.validateCodeGenerator.get(name);
        C code = (C)validateCodeGenerator.generate(request.getRequest());

        sessionStrategy.setAttribute(request,SESSION_KEY_PREFIX+type,code.getCode());

        send(request,code);
    }



    @Override
    public void validate() {

    }

    public abstract void send(ServletWebRequest request,C code);

}
